package com.svse.controller;


import com.alibaba.fastjson.JSONObject;
import com.svse.dao.DemoRepository;
import com.svse.entity.DemoEntity;
import com.svse.entity.DemoJpaEntity;
import com.svse.services.DemoJpaService;
import com.svse.services.DemoService;
import com.svse.services.impl.DemoServiceImpl;
import com.svse.util.RedisUtil;
import com.svse.util.Result;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李宇航
 * @since 2019-08-18
 */
@Controller
@RequestMapping("/demoEntity")
public class DemoController {
    private static Logger logger = LoggerFactory.getLogger(DemoController.class);
    @Autowired
    private DemoService demoService;
    @Autowired
    private RedisUtil redisService;

    @Autowired
    private DemoRepository demoRepository;
    @Autowired
    private DemoJpaService demoJpaService;



   // @AutoLog("进行demo分页查询测试操作")
    @ApiOperation(value = "分页查询", notes = "分页查询用户信息")
    @RequestMapping(value = "/demoTest", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Result findList() {
        JSONObject jsonObject = new JSONObject();
        logger.info("info信息");
        logger.error("error信息");
        logger.debug("debug信息");
        logger.warn("warn信息");
        jsonObject.put("data", demoService.page(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(1, 10)));
        return Result.ok(jsonObject);
    }

    @RequestMapping("/demogetOne")
    @ResponseBody
    public String demogetOne() {
        int a = 10;
        int b = 0;
        try {
            int c = a / b;
        } catch (Exception e) {

        }
        return "java好累";
    }

    @RequestMapping("/saveBatch")
    @ResponseBody
    public void saveBatch() {
        List<DemoEntity> demoEntityList=new ArrayList<DemoEntity>();
        for (int i = 0; i < 5; i++) {
            DemoEntity demoEntity = new DemoEntity();
            demoEntity.setName("test" + i);
            if (i % 2 == 0) {
                demoEntity.setSex("女");
            } else {
                demoEntity.setSex("男");
            }
            if(i==3){
                demoEntity.setSex("人妖人妖");
            }
            demoEntity.setEmail("test@qq.com");
            demoEntity.setAge((15 + i));
            demoEntity.setContent("这是第" + i + "条数据");
            demoEntity.setBirthday(LocalDate.now());
            demoEntityList.add(demoEntity);
        }
        demoService.saveBatch(demoEntityList);
    }


    @RequestMapping("/saveList")
    @ResponseBody
    public void saveList() {
        List<DemoJpaEntity> demoJpaEntityList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            DemoJpaEntity demoJpaEntity = new DemoJpaEntity();
            demoJpaEntity.setName("test" + (11 + i));
            if (i % 2 == 0) {
                demoJpaEntity.setSex("女");
            } else {
                demoJpaEntity.setSex("男");
            }
            demoJpaEntity.setEmail("test@qq.com");
            demoJpaEntity.setAge((15 + i));
            demoJpaEntity.setContent("这是第" + (11 + i) + "条数据");
            demoJpaEntity.setBirthday(new Date());
            demoJpaEntityList.add(demoJpaEntity);
        }
        demoRepository.saveAll(demoJpaEntityList);
    }

    @RequestMapping("/getOne")
    @ResponseBody
    public DemoJpaEntity getOne(Long id) {
        return demoRepository.getOne(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(Long id) {
        DemoJpaEntity demoJpaEntity = demoRepository.getOne(id);
        demoJpaEntity.setContent("我是被修改过后的数据");
        demoRepository.save(demoJpaEntity);
    }

    @RequestMapping("/findAll")
    @ResponseBody
    @Cacheable(value = "DemoJpaEntity_list")
    public List<DemoJpaEntity> list() {
        return demoRepository.findAll();
    }

    @RequestMapping("/findByPage")
    @ResponseBody
    @Cacheable(value = "DemoJpaEntity_list_findByPage")
    public Page<DemoJpaEntity> findByPage(int pageNumber, int pageSize) {
        List<String> sortFields = new ArrayList<>();
        sortFields.add("age");
        sortFields.add("id");
        Sort sort = new Sort(Sort.Direction.ASC, sortFields);
        Pageable pageable = new PageRequest(pageNumber, pageSize, sort);
        Page<DemoJpaEntity> DemoJpaEntitys = demoRepository.findAll(pageable);
        return DemoJpaEntitys;
    }


    @RequestMapping("/deleteById")
    @ResponseBody
    public void deleteById(Long id) {
        demoRepository.deleteById(id);
    }
}

